package com.xmg.client.mgrsite.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xmg.shop.api.domain.CatalogProperty;
import com.xmg.shop.api.domain.CatalogPropertyValue;
import com.xmg.shop.api.service.ICatalogPropertyService;
import com.xmg.shop.api.util.AjaxResult;
import com.xmg.shop.api.vo.ProductCatalogPropertyVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
public class CatalogPropertyController {
    @Reference
    private ICatalogPropertyService catalogPropertyService;

    @RequestMapping("/catalogProperty")
    public String catalogProperty() {
        return "property/property";
    }

    @RequestMapping("/catalogProperty/get/{id}")
    public String getCatalogProperty(@PathVariable("id") Long catalogId, Model model) {
        List<CatalogProperty> list = catalogPropertyService.getCatalogProperty(catalogId);
        model.addAttribute("list", list);
        return "property/property_list";
    }

    @RequestMapping(value="/catalogProperty/propertyValue/get/{catalogId}", method=RequestMethod.GET)
    public String getCatalogProperty(Model model, @PathVariable("catalogId") Long catalogId)
    {
        List<ProductCatalogPropertyVo> productCatalogPropertyList = new ArrayList();

        List<CatalogProperty> catalogPropertyList = this.catalogPropertyService.getCatalogProperty(catalogId);
        for (CatalogProperty catalogProperty : catalogPropertyList)
        {
            ProductCatalogPropertyVo productCatalogPropertyVo = new ProductCatalogPropertyVo();

            productCatalogPropertyVo.setCatalogPropertyName(catalogProperty.getName());

            productCatalogPropertyVo.setType(catalogProperty.getType());

            List<CatalogPropertyValue> catalogPropertyValue = this.catalogPropertyService.getCatalogPropertyValue(catalogProperty.getId());

            productCatalogPropertyVo.setPropertyValueList(catalogPropertyValue);

            productCatalogPropertyList.add(productCatalogPropertyVo);
        }
        model.addAttribute("productCatalogPropertyList", productCatalogPropertyList);
        return "product/catalog_property_panel";
    }

    @RequestMapping(value = "/catalogProperty/add",method = RequestMethod.GET)
    public String property_input(CatalogProperty catalogProperty,Model model) {
        if (catalogProperty.getId() != -1) {
            catalogProperty = catalogPropertyService.get(catalogProperty.getId());
        }
        model.addAttribute("catalogProperty", catalogProperty);
        return "property/property_save";
    }
    @RequestMapping(value = "/catalogProperty/add",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult property_saveOrUpdate(@ModelAttribute("catalogProperty") CatalogProperty catalogProperty) {
        AjaxResult result = null;
        try {
            catalogPropertyService.saveOrUpdate(catalogProperty);
            result = new AjaxResult(true, "保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/catalogProperty/delete")
    public String deleteProperty(Long id, Long catalogId) {
        catalogPropertyService.delete(id);
        return "forward:/catalogProperty/get/"+catalogId;
    }
}
